Imports System.Data.OleDb

Public Class frmElencoContratti

    Private keyDefunto As Integer
    Public Property codiceDefunto()
        Get
            Return keyDefunto
        End Get
        Set(ByVal value)
            keyDefunto = value
        End Set
    End Property

    Private keyContratto As Integer
    Public Property codiceContratto()
        Get
            Return keyContratto
        End Get
        Set(ByVal value)
            keyContratto = value
        End Set
    End Property

    Private Sub frmElencoContratti_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dsDataset As New DataSet
        Dim dvVista As DataView
        Dim cnNW As New OleDbConnection(strConnessioneClienti)
        Dim strSql As String = "SELECT KEY_ID, (CONTRATTO_ALFA & CONTRATTO_NUM) AS CONTRATTO, NUM_LAMPADE AS [NUMERO LAMPADE], (CANONEDA) AS MESE FROM CONTRATTI WHERE CODICE_DEFUNTO=" & codiceDefunto
        Dim daLettore As New OleDbDataAdapter(strSql, cnNW)

        daLettore.Fill(dsDataset, "contratti")
        If dsDataset.Tables("contratti").Rows.Count <> 0 Then
            dvVista = dsDataset.Tables("contratti").DefaultView
            flxGriglia.DataSource = dvVista
            flxGriglia.Columns(0).Visible = False
            dsDataset = Nothing
            cnNW.Close()
        Else
            Me.Close()
        End If
    End Sub

    Private Sub flxGriglia_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles flxGriglia.DoubleClick
        Dim rowIndex As Integer = flxGriglia.SelectedCells(0).RowIndex
        If (rowIndex <> -1 And rowIndex <> flxGriglia.RowCount) And flxGriglia.SelectedCells(0).ColumnIndex = 0 Then
            keyContratto = flxGriglia.Item(0, rowIndex).Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub flxGriglia_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles flxGriglia.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = Windows.Forms.DialogResult.No
        End If
    End Sub

    Private Sub flxGriglia_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles flxGriglia.CellFormatting

        If flxGriglia.Columns(e.ColumnIndex).Index = 3 Then
            If e IsNot Nothing Then
                If e.Value IsNot Nothing Then
                    e.Value = UCase(MonthName(e.Value + 1))
                    e.FormattingApplied = True
                End If
            End If
        End If

        If flxGriglia.Columns(e.ColumnIndex).Index = 1 Then
            If e IsNot Nothing Then
                If e.Value IsNot Nothing And (e.Value = "A0" Or e.Value = "B0") Then
                    e.Value = Mid(e.Value, 1, 1)
                    e.FormattingApplied = True
                End If
            End If
        End If


    End Sub

End Class